package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.ReportMapper;
import com.itheima.pojo.ClazzCountOption;
import com.itheima.pojo.JobOption;
import com.itheima.pojo.OperateLog;
import com.itheima.pojo.PageBean;
import com.itheima.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

// 进行各种统计的业务层实现类
@Service
public class ReportServiceImpl implements ReportService {
    // 业务层依赖Mapper
    @Autowired
    private ReportMapper reportMapper;

    /**
     * 统计员工职位
     * @return
     */
    @Override
    public JobOption empJobData() {
        // 调用Mapper查询到员工职位数据
        List<Map<String, Object>> list = reportMapper.empJobData();

        // 获取查询的数据,封装到JobOption对象中
        // stream().map: 改变流中的类型
        // List<job的值String>
        // List<Object> jobList = list.stream().map(map -> {
        //     return map.get("job");
        // }).collect(Collectors.toList()); // [教研主管, 讲师, 学工主管, 班主任, 咨询师, 其他]

        List<Object> jobList = new ArrayList<>();
        for (Map<String, Object> map : list) {
            Object job = map.get("job");
            jobList.add(job);
        }

        // List<count的值String>
        List<Object> countList = list.stream().map(map -> {
            return map.get("count");
        }).collect(Collectors.toList());
        // [1, 15, 1, 12, 7, 1]

        JobOption jobOption = new JobOption(jobList, countList);

        // 返回对象
        return jobOption;
    }

    /**
     * 统计员工性别
     * @return
     */
    @Override
    public List<Map> empGenderData() {
        // 调用Mapper统计员工性别
        List<Map> list = reportMapper.empGenderData();
        return list;
    }

    @Override
    public List<Map> getStudentDegreeData() {
        return reportMapper.countStudentDegreeData();
    }

    @Override
    public ClazzCountOption getStudentCountData() {
        List<Map<String, Object>> countList = reportMapper.getStudentCount();
        if(!CollectionUtils.isEmpty(countList)){
            List<Object> clazzList = countList.stream().map(map -> {
                return map.get("cname");
            }).toList();

            List<Object> dataList = countList.stream().map(map -> {
                return map.get("scount");
            }).toList();

            return new ClazzCountOption(clazzList, dataList);
        }
        return null;
    }

    @Override
    public PageBean getLogs(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);

        Page<OperateLog> logs = reportMapper.getLogs(page, pageSize);

        return new PageBean(logs.getTotal(), logs.getResult());
    }
}
